package one.stand.controller;

import com.jmp.api.admin.dto.CompanyPlatformAddChildRequest;
import com.jmp.api.admin.dto.CompanyPlatformAddRequest;
import com.jmp.api.admin.dto.CompanyPlatformListResponse;
import com.jmp.api.admin.dto.CompanyPlatformRemoveRequest;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import one.stand.dto.ConvertUtils;
import one.stand.mapper.CompanyPlatformMapperApiCrmPcExt;
import one.stand.model.Company;
import one.stand.model.CompanyPlatform;
import one.stand.model.ResultModel;
import one.stand.service.CompanyBaseService;
import one.stand.service.CompanyPlatformService;
import one.stand.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("adminApi/company/platform")
public class CompanyPlatformController {
    @Autowired
    private CompanyPlatformMapperApiCrmPcExt companyPlatformMapperApiCrmPcExt;
    @Autowired
    private CompanyBaseService companyBaseService;
    @Autowired
    private CompanyPlatformService companyPlatformService;

    /**
     * 企业平台列表
     */
    @PostMapping("list")
    @SecurityAuthCheck
    public ResultModel<CompanyPlatformListResponse> list() {

        List<CompanyPlatform> companyPlatforms = companyPlatformMapperApiCrmPcExt.selectPlatform();
        List<CompanyPlatformListResponse.Platform> platformList = ConvertUtils.list(companyPlatforms, companyPlatform -> {
            Company company = companyBaseService.getById(companyPlatform.getCompanyId());
            return CompanyPlatformListResponse.Platform.builder()
                    .id(companyPlatform.getId())
                    .type(companyPlatform.getType())
                    .name(company.getCompanyName())
                    .clientKey(company.getCompanyKey())
                    .ctime(DateUtil.format(DateUtil.formatter, companyPlatform.getCreateTime()))
                    .build();
        });

        CompanyPlatformListResponse response = CompanyPlatformListResponse.builder()
                .platformList(platformList)
                .build();
        return ResultModel.success(response);
    }

    /**
     * 新增
     */
    @PostMapping("add")
    @SecurityAuthCheck
    public ResultModel add(@RequestBody CompanyPlatformAddRequest request) {

        companyPlatformService.add(request.getType(), request.getCompanyId());

        return ResultModel.success();
    }


    /**
     * 新增关联企业
     */
    @PostMapping("add/child")
    @SecurityAuthCheck
    public ResultModel addChild(@RequestBody CompanyPlatformAddChildRequest request) {

        companyPlatformService.addChild(request.getPlatformId(), request.getCompanyId());

        return ResultModel.success();
    }

    /**
     * 删除
     */
    @PostMapping("remove")
    @SecurityAuthCheck
    public ResultModel remove(@RequestBody CompanyPlatformRemoveRequest request) {

        companyPlatformService.remove(request.getId());

        return ResultModel.success();
    }
}
